package com.buba.controller.yonghu;

import com.buba.pojo.Area;
import com.buba.pojo.Dept;
import com.buba.pojo.User;
import com.buba.service.yonghu.YonghuService;
import com.buba.utils.MyContent;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RequestMapping("yonghu")
@Controller
public class YonghuController {
    Logger log= LogManager.getLogger();
    @Autowired
    @Qualifier("yonghuService")
    YonghuService yonghuService;

    /**
     * 跳转至用户列表页面
     * @return
     */
    @RequestMapping("toUserList")
    public String toUserList(){
        return "/yonghu/userList";
    }

    /**
     * 查询用户列表
     * @param user
     * @return
     */
    @RequestMapping("listUser")
    @ResponseBody
    public Map<String,Object> listUser(@RequestBody User user){
        Map<String,Object> m=new HashMap<>(16);
        List<User> userList=yonghuService.listUser(user);
        if(userList.size()>0){
            m.put(MyContent.CODE,"0000");
            m.put(MyContent.MSG,"查询成功");
            m.put(MyContent.DATA,userList);
        }else{
            m.put(MyContent.CODE,"9999");
            m.put(MyContent.MSG,"查询失败");
            m.put(MyContent.DATA,userList);

        }
        return m;
    }

    /**
     * 查询区域列表
     * @return
     */
    @RequestMapping("listArea")
    @ResponseBody
    public Map<String,Object> listArea(){
        Map<String,Object> m=new HashMap<>(16);
        List<Area> areaList=yonghuService.listArea();
        m.put(MyContent.CODE,"0000");
        m.put(MyContent.MSG,"查询成功");
        m.put(MyContent.DATA,areaList);
        return m;
    }

    /**
     * 查询部门列表
     * @param areaid
     * @return
     */
    @RequestMapping("listDept")
    @ResponseBody
    public Map<String,Object> listDept(@RequestBody Integer areaid){
        Map<String,Object> m=new HashMap<>(16);
        List<Dept> deptList=yonghuService.listDept(areaid);
        m.put(MyContent.CODE,"0000");
        m.put(MyContent.MSG,"查询成功");
        m.put(MyContent.DATA_ONE,deptList);
        return m;
    }

    /**
     * 跳转至用户添加页面
     * @return
     */
    @RequestMapping("toInsertUser")
    public String toInsertUser(){
        return "/yonghu/insertUser";
    }

    /**
     * 添加用户
     * @param user
     * @param model
     * @param session
     * @return
     */
    @RequestMapping("insertUser")
    public String insertUser(User user, Model model, HttpSession session){
        User userSession=(User)session.getAttribute(MyContent.DATA);
        user.setCreatedby(userSession.getId());
        user.setModifyby(userSession.getId());
        Map<String,Object> m=new HashMap<>(16);
        User user1=yonghuService.getUserByCode(user.getUsercode());
        if(user1==null){
            m.put(MyContent.CODE,"0000");
            m.put(MyContent.MSG,"添加成功");
            yonghuService.insertUser(user);
            return "/yonghu/userList";
        }else{
            m.put(MyContent.CODE,"0000");
            m.put(MyContent.MSG,"添加失败");
            model.addAttribute(MyContent.MSG,"该用户已存在");
            return "/yonghu/insertUser";
        }
    }

    /**
     * 显示删除模态框的信息
     * @param id
     * @return
     */
    @RequestMapping("getUserInfo")
    @ResponseBody
    public Map<String,Object> getUserInfo(@RequestBody Integer id){
        Map<String,Object> m=new HashMap<>(16);
        User user=yonghuService.getUserInfo(id);
        if(user!=null){
            m.put(MyContent.CODE,"0000");
            m.put(MyContent.MSG,"查询成功");
            m.put(MyContent.DATA_ONE,user);
        }else {
            m.put(MyContent.CODE,"9999");
            m.put(MyContent.MSG,"查询失败");
            m.put(MyContent.DATA_ONE,user);
        }
        return m;
    }
    /**
     * 根据id删除机构信息
     */
    @RequestMapping("deleteUserById")
    @ResponseBody
    public Map<String,Object> deleteUserById(@RequestBody Integer id){
        Map<String,Object> m=new HashMap<>(16);
        int i=yonghuService.deleteUserById(id);
        if(i>0){
            m.put(MyContent.CODE,"0000");
            m.put(MyContent.MSG,"删除成功");
        }else {
            m.put(MyContent.CODE,"9999");
            m.put(MyContent.MSG,"删除失败");
        }
        return m;
    }

    /**
     * 跳转至修改页面
     */
    @RequestMapping("toUpdateUser")
    public String toUpdateUser(Integer id, Model model){
        model.addAttribute("id",id);
        return "yonghu/updateUser";
    }

    /**
     * 修改中支机构
     * @param user
     * @return
     */
    @RequestMapping("updateUser")
    public String updateUser(User user,Model model,HttpSession session){
        User user1=(User)session.getAttribute(MyContent.DATA);
        Integer id=user1.getId();
        Date date=new Date();
        user.setModifydate(date);
        user.setModifyby(id);
        Map<String,Object> m=new HashMap<>(16);
        int i=yonghuService.updateUser(user);
        if(i>0){
            m.put(MyContent.CODE,"0000");
            m.put(MyContent.MSG,"修改成功");
            return "/yonghu/userList";
        }else{
            m.put(MyContent.CODE,"9999");
            m.put(MyContent.MSG,"修改失败");
            model.addAttribute("id",user.getId());
            return "/yonghu/updateUser";
        }
    }

    @RequestMapping("resetPassword")
    @ResponseBody
    public Map<String,Object> resetPassword(@RequestBody Integer id){
        Map<String,Object> m=new HashMap<>(16);
        int i=yonghuService.resetPassword(id);
        if(1>0){
            m.put(MyContent.CODE,"0000");
            m.put(MyContent.MSG,"重置成功");
        }else{
            m.put(MyContent.CODE,"9999");
            m.put(MyContent.MSG,"重置失败");

        }
        return m;
    }
}
